package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyContent;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.article.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.handler.StrategyStatisHandler;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.IStrategySearchService;
import cn.wolfcode.luowowo.search.template1.StrategyTemplate;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.catalina.User;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Administrator on 2019/7/10.
 */
@Controller
@RequestMapping("strategy")
public class StrategyController {
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyContentService strategyContentService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyTagService strategyTagService;
    @Reference
    private IStrategyCommentService strategyCommentService;
    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;
    @Reference
    private IStrategySearchService strategySearchService;
    @RequestMapping("detail")
    public String detail(Long id,Model model){
        //detail
        StrategyDetail strategyDetail = strategyDetailService.get(id);
        StrategyContent strategyContent = strategyContentService.get(strategyDetail.getId());
        strategyDetail.setStrategyContent(strategyContent);
        model.addAttribute("detail",strategyDetail);
        //同级阅读数量
        strategyStatisRedisService.viewIncrease(StrategyStatisHandler.STRATEGY_STATIS_VO,id);
        model.addAttribute("vo",strategyStatisRedisService.getStatisVo(id));
        strategyStatisRedisService.addScore(StrategyStatisHandler.STRATEGY_STATIS_COMMEND_HOT,id,1);
        return "strategy/detail";
    }
    //查看所有攻略明细
    @RequestMapping("list")
    public String list(Model model,@ModelAttribute("qo") StrategyDetailQuery qo){
        //toasts
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.get(toasts.size() - 1);
        toasts.remove(dest);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);
        //tags
        model.addAttribute("tags",strategyTagService.list());
        //pageInfo
        model.addAttribute("pageInfo", strategyDetailService.query(qo));
        return "strategy/list";
    }
    @RequestMapping("commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment strategyComment, @UserParam UserInfo userInfo){
        strategyComment.setUserId(userInfo.getId());
        strategyComment.setUsername(userInfo.getNickname());
        strategyComment.setCity(userInfo.getCity());
        strategyComment.setLevel(userInfo.getLevel());
        strategyComment.setHeadUrl(userInfo.getHeadImgUrl());
        strategyCommentService.save(strategyComment);

        //同级评论数增加
        strategyStatisRedisService.replyIncrease(StrategyStatisHandler.STRATEGY_STATIS_VO,strategyComment.getDetailId());
        StrategyStatisVO statisVo = strategyStatisRedisService.getStatisVo(strategyComment.getDetailId());
        strategyStatisRedisService.addScore(StrategyStatisHandler.STRATEGY_STATIS_COMMEND_HOT,strategyComment.getDetailId(),1);
        return new AjaxResult().
                addData(strategyStatisRedisService.getStatisVo(strategyComment.getDetailId()).getReplynum());
    }
    @RequestMapping("comment")
    public String comment(Model model,@ModelAttribute("qo") StrategyCommentQuery qo){
        Page<StrategyComment> page = strategyCommentService.query(qo);
        model.addAttribute("page",page);
        return "strategy/commentTpl";
    }
    @RequestMapping("commentThumbUp")
    @ResponseBody
    @RequireLogin
    public Object commentThumbUp(String toid,Long fromid){
        boolean ret = strategyCommentService.thumbUp(toid,fromid);
        return new AjaxResult(ret,"");
    }
    @RequestMapping("favor")
    @ResponseBody
    //@RequireLogin
    public Object favor(Long sid, @UserParam UserInfo userInfo){
        if (userInfo==null){
            return new AjaxResult("清先登录",AjaxResult.CODE_TYPE_UNLOGIN);
        }
        //前台点击"收藏按钮",后台收藏或者取消收藏
        boolean ret = strategyStatisRedisService.favor(StrategyStatisHandler.STRATEGY_STATIS_FAVOR,sid,userInfo.getId());
        int score = 1;
        if (!ret){
            score = -1;
        }
        strategyStatisRedisService.addScore(StrategyStatisHandler.STRATEGY_STATIS_COMMEND_SORT,sid,score);
        return new AjaxResult(ret,"").addData(strategyStatisRedisService.getStatisVo(sid));
    }
    //顶
    @RequestMapping("strategyThumbup")
    @ResponseBody
    //@RequireLogin
    public Object strategyThumbup(Long sid, @UserParam UserInfo userInfo){
        if (userInfo==null){
            return new AjaxResult("请先登录",AjaxResult.CODE_TYPE_UNLOGIN);
        }
        boolean ret = strategyStatisRedisService.thumbup(StrategyStatisHandler.STRATEGY_STATIS_THUMBSUP,sid,userInfo.getId());
        if (ret){
            strategyStatisRedisService.addScore(StrategyStatisHandler.STRATEGY_STATIS_COMMEND_SORT,sid,1);
        }
        return new AjaxResult(ret,"").addData(strategyStatisRedisService.getStatisVo(sid));
    }
    @RequestMapping("searchPage")
    public String searchPage(Model model, @ModelAttribute("qo") SearchQueryObject qo){
        Page<StrategyTemplate> page =strategySearchService.query(qo);
        model.addAttribute("page",page);
        return "strategy/searchPageTpl";
    }
}
